package com.txunda.zhuohired.aty.user.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.ants.theantsgo.util.L;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.util.wheel.NumericWheelAdapter;
import com.txunda.zhuohired.util.wheel.OnWheelChangedListener;
import com.txunda.zhuohired.util.wheel.OnWheelScrollListener;
import com.txunda.zhuohired.util.wheel.WheelView;

import java.util.Calendar;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/2 14:38
 * 功能描述：选择时间
 * 联系方式：jiahuanchen@126.com
 */

public class SelectTimeAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * 年
     */
    @ViewInject(R.id.wl_select_time_year)
    private WheelView wl_select_time_year;
    /**
     * 月
     */
    @ViewInject(R.id.wl_select_time_month)
    private WheelView wl_select_time_month;
    /**
     * 日
     */
    @ViewInject(R.id.wl_select_time_day)
    private WheelView wl_select_time_day;

    private int curYear;
    private int curMonth;
    private String date = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("设置时间");
        initWheelView();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_select_time;
    }

    @Override
    @OnClick({R.id.tv_select_time_sure})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_select_time_sure://确定
                L.e("====================",date);
                Intent intent = new Intent();
                intent.putExtra("time_date",date);
                setResult(RESULT_OK,intent);
                finish();
                break;
        }
    }

    @Override
    protected void initialized() {

    }

    @Override
    protected void requestData() {

    }
    private void initWheelView() {
        Calendar c = Calendar.getInstance();
        curYear = c.get(Calendar.YEAR);
        curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
        int curDate = c.get(Calendar.DATE);
        String month = "";
        String day = "";
        if (curMonth < 10) {
            month = "0" + curMonth;
        } else {
            month = "" + curMonth;
        }
        if (curDate < 10) {
            day = "0" + curDate;
        } else {
            day = "" + curDate;
        }
        date = curYear + "/" + month + "/" + day;


        NumericWheelAdapter numericWheelAdapterStart1 = new NumericWheelAdapter(this, 2000, 2100);
        numericWheelAdapterStart1.setLabel("年");
        wl_select_time_year.setViewAdapter(numericWheelAdapterStart1);
        numericWheelAdapterStart1.setTextColor(R.color.black);
        numericWheelAdapterStart1.setTextSize(20);
        wl_select_time_year.setCyclic(true);//是否可循环滑动
        wl_select_time_year.addScrollingListener(startScrollListener);
        wl_select_time_year.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                curYear = newValue + 2000;
                initStartDayAdapter();
            }
        });

        NumericWheelAdapter numericWheelAdapterStart2 = new NumericWheelAdapter(this, 1, 12, "%02d");
        numericWheelAdapterStart2.setLabel("月");
        wl_select_time_month.setViewAdapter(numericWheelAdapterStart2);
        numericWheelAdapterStart2.setTextColor(R.color.black);
        numericWheelAdapterStart2.setTextSize(20);
        wl_select_time_month.setCyclic(true);
        wl_select_time_month.addScrollingListener(startScrollListener);
        wl_select_time_month.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                curMonth = newValue + 1;
                initStartDayAdapter();
            }
        });
        initStartDayAdapter();


        wl_select_time_year.setCurrentItem(curYear - 2000);
        wl_select_time_month.setCurrentItem(curMonth - 1);
        wl_select_time_day.setCurrentItem(curDate - 1);


    }

    private void initStartDayAdapter() {
        NumericWheelAdapter numericWheelAdapterStart3 = new NumericWheelAdapter(this, 1, getDay(curYear, curMonth)
                , "%02d");
        numericWheelAdapterStart3.setLabel("日");

        wl_select_time_day.setViewAdapter(numericWheelAdapterStart3);
        numericWheelAdapterStart3.setTextColor(R.color.black);
        //        numericWheelAdapterStart3.setBackGroundColor(R.color.colorPrimary);
        numericWheelAdapterStart3.setTextSize(20);
        wl_select_time_day.setCyclic(true);
        wl_select_time_day.addScrollingListener(startScrollListener);
    }

    OnWheelScrollListener startScrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {
        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            int n_year = wl_select_time_year.getCurrentItem() + 2000;//年
            int n_month = wl_select_time_month.getCurrentItem() + 1;//月
            int n_day = wl_select_time_day.getCurrentItem() + 1;//日
            String month = "";
            String day = "";
            if (n_month < 10) {
                month = "0" + n_month;
            } else {
                month = "" + n_month;
            }
            if (n_day < 10) {
                day = "0" + n_day;
            } else {
                day = "" + n_day;
            }
            date = n_year + "/" + month + "/" + day;
        }
    };

    /**
     * 根据年月获得 这个月总共有几天
     *
     * @param year
     * @param month
     * @return
     */
    public int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }

}
